function triggerform(name){
	var field = document.getElementById('metid');
	
	if(field == null){
		alert('cannot find the edit field with the id metid');
	}
	else {
		field.value = name;
		document.forms[0].submit();
	}
}

function triggeredit(id, name){
	// fill out the id
	var idfield = document.getElementById('idedit'); 
	
	if(idfield == null){
		alert('field with id idedit'); 
	}
	else {
		idfield.value = id; 
		
		// trigger form with name
		triggerform(name);
	}
}

function none(){
	
}

function toggleSelected(event){
	var evt = event || window.event;
	var source = evt.target || evt.srcElement;
	
	var cell = jQuery(source).parent('TD'); 
	var row = jQuery(cell).parent('TR');
	
	cell.toggleClass('selected');
	var index = jQuery('TD', row).index(cell);
	var has = cell.hasClass('selected'); 
	
	var otherCell = jQuery('TD', row.prev('TR'))[index];
	
	var items = jQuery('INPUT', cell);
	var dummy = items[0];
	var selected = items[1];
	
	if(has){
		jQuery(otherCell).addClass('selected');
		selected.value = dummy.value;
	}
	else {
		jQuery(otherCell).removeClass('selected');
		selected.value = '';
	}
}

function clicker(eventObject){
	if(eventObject != null){
		var code = eventObject.keyCode;
		
		if(code == 13){
			var elements = jQuery('input.enter');
			
			elements.click();
		}
		else if(code == 27){
			var elements = jQuery('input.escape');
			
			elements.click();
		}
	}
}

function selectFirst(){
	// select input that are not hidden or disabled
	var items = jQuery('input[type!="hidden"][type!="button"][disabled!="true"]', jQuery('div.content'));
	
	if(items && items.length > 0){
		var element = items[0];
		
		element.focus();
		element.select();
	}
	
	// attach the event stuff
	items.keydown(clicker);
}

function moveline(isup){
	// search for all the checkboxes checked
	var checkboxes = jQuery("table#list input:checked");
	
	if(checkboxes.length == 0){
		alert('Please go ahead and select at least one album to be moved');
	}
	else if(checkboxes.length > 1){
		alert('Please select one and only one album to move up or down');
	}
	else {
		for(var i = 0; i < checkboxes.length; i ++){
			var check = checkboxes[i];
			
			// get the id
			var id = check.value; 
			var tr = jQuery(jQuery(check).parents('TR')[0]);
			var other = isup == 1 ? tr.prev('TR') : tr.next('TR');
			
			if(other && other.length && other.length > 0){
				var otherTr = other[0];
				
				var otherId = jQuery('input:checkbox', otherTr)[0].value;
				
				jQuery.ajax({
					url: "switchalbums.php", 
					context: [isup, tr, otherTr], 
					data: {first: id, second: otherId}, 
					dataType: 'json', 
					success: processResponse
				});
			}
		}
	}
}

function processResponse(data, status, obj){
	if(data.success){
		var isup = this[0];
		var tr = this[1];
		var otherTr = this[2];
		
		if(isup == 1){
			jQuery(otherTr).insertAfter(tr);
		}
		else {
			jQuery(otherTr).insertBefore(tr);
		}
	}
	else {
		var error = 'error: ';
		
		if(data.error){
			error = error + data.error; 
		}
		
		alert(error);
	}
}

function doConfirm(message, name){
	if(confirm(message)){
		triggerform(name);
	}
}

function redocheckboxes(id){
	// get first the checkboxes in the tbody
	var list = jQuery('tbody input', jQuery(id));
	var check = true; 
	
	if(list.length && list.length > 0){
		for(var i = 0; i < list.length; i++){
			if(! list[i].checked){
				check = false; 
				
				break;
			}
		}
	}
	else {
		check = false; 
	}
	
	jQuery('thead input', jQuery(id))[0].checked = check; 
}


function tsl(event){
	var evt = event || window.event; 
	var source = evt.target || evt.srcElement;
	
	// get the table associated with the element
	var table = jQuery(source).parents('TABLE');
	
	var chk = source.checked; 
	var list = jQuery('tbody input', table); 
	
	if(list.length && list.length > 0){
		for(var i = 0; i < list.length; i ++){
			list[i].checked = chk;
		}
	}
}

function tslb(event){
	// first determine the check value
	var evt = event || window.event; 
	var source = evt.target || evt.srcElement;

	var table = jQuery(source).parents('table')[0];
	var checks = jQuery('tbody input', table);
	
	var chk = true; 
	
	if(checks.length && checks.length > 0){
		for(var i = 0; i < checks.length; i ++){
			if(! checks[i].checked){
				chk = false;
			}
		}
	}
	
	jQuery('thead input', table)[0].checked = chk;
}
